package com.codemart.lanze.modules.log.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codemart.lanze.modules.log.mapper.LogMapper;
import com.codemart.lanze.modules.log.model.Log;
import com.codemart.lanze.modules.log.service.LogService;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 */
@Transactional
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

	@Transactional(readOnly = true)
	@Override
	public IPage<Log> findPage(Integer pageNum, Integer pageSize, String username, String startTime, String endTime) {

		QueryWrapper<Log> qw = new QueryWrapper<>();

		if (StringUtils.isNotEmpty(username)) {
			qw.like("username", "%" + username + "%");
		}
		if (StrUtil.isNotEmpty(startTime)) {
			qw.ge("create_time", DateUtil.beginOfDay(DateUtil.parse(startTime)));
		}
		if (StrUtil.isNotEmpty(endTime)) {
			qw.le("create_time", DateUtil.endOfDay(DateUtil.parse(endTime)));
		}

		// 倒序
		qw.orderByDesc("create_time");

		IPage<Log> iPage = this.page(new Page<>(pageNum, pageSize), qw);

		return iPage;
	}
}
